/**   
 * @Title: CityServiceImpl.java
 * @Package com.cityoneyes.service.impl
 * @Description: TODO
 * @author Rick
 * @date 2011-8-4 下午09:51:14
 * @version V1.0   
 */
package com.cityoneyes.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cityoneyes.bean.Area;
import com.cityoneyes.bean.City;
import com.cityoneyes.bean.Position;
import com.cityoneyes.dao.IAreaDAO;
import com.cityoneyes.dao.IPositionDAO;
import com.cityoneyes.service.ICityService;

/**
 * @ClassName: CityServiceImpl
 * @Description: 城市相关的Service实现类
 * @author Rick
 * @date 2011-8-4 下午09:51:14
 */
@Service
@SuppressWarnings("unchecked")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
public class CityServiceImpl extends BaseServiceImpl implements ICityService {

	// 数据容器
	private static Map<Integer, City> map = null;
	
	@Autowired
	private IAreaDAO areaDao;
	@Autowired
	private IPositionDAO positionDao;
	
	@Override
	public List<City> init() {
		List<City> list = super.getAll(City.class);
		if (list != null) {
			map = new HashMap<Integer, City>();
			for (City obj : list) {
				map.put(obj.getId(), obj);
			}
		}
		return list;
	}
	
	@Override
	public List<City> getAllCity() {
		if (map == null) {
			return init();
		}
		return (List<City>) map.values();
	}
	
	@Override
	public City getCityById(int id) {
		if (map == null) {
			init();
		}
		City city = map.get(id);
		if (city == null) {
			// 重新查找
			city = (City)super.getById(City.class, id);
			if (city == null) {
				return null;
			}
			map.put(id, city);
		}
		return city;
	}

	@Override
	public List<Position> getAreaPositions(int areaId) {
		return positionDao.getAreaPositions(areaId);
	}

	@Override
	public List<Area> getCityAreas(int cityId) {
		return areaDao.getCityAreas(cityId);
	}
}
